package com.tsj.spzx.pay.service.Impl;

import com.tsj.spzx.client.feign.ProductFeignClient;
import com.tsj.spzx.model.dto.product.SkuSaleDto;
import com.tsj.spzx.model.entity.order.OrderInfo;
import com.tsj.spzx.model.entity.order.OrderItem;
import com.tsj.spzx.model.entity.pay.PaymentInfo;
import com.tsj.spzx.order.feign.OrderFeignClient;
import com.tsj.spzx.pay.mapper.PaymentInfoMapper;
import com.tsj.spzx.pay.service.PaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private OrderFeignClient orderFeignClient;
    @Autowired
    private ProductFeignClient productFeignClient;

    /**
     * 保存支付记录
     * @param orderNo
     * @return
     */
    @Override
    public PaymentInfo savePaymentInfo(String orderNo) {
        // 1、先根据订单编号 查询支付记录表中是否有数据 有则直接返回 无则新增
        PaymentInfo paymentInfo = paymentInfoMapper.findPaymentInfoByOrderNo(orderNo);
        if (paymentInfo == null){
            // 表示没有数据，那我们就新增
            // 2、todo 远程调用 根据 订单编号查询订单信息，用于数据封装
            OrderInfo orderInfo = orderFeignClient.getOrderInfoByOrderNo(orderNo);
            paymentInfo = new PaymentInfo();
            // 开始数据封装
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setOrderNo(orderInfo.getOrderNo());
            paymentInfo.setPayType(orderInfo.getPayType());   // 付款方式

            // 获取orderInfo中的订单项列表
            String content = "";
            for (OrderItem orderItem : orderInfo.getOrderItemList()) {
                content += orderItem.getSkuName() + " ";
            }
            paymentInfo.setContent(content);
            paymentInfo.setAmount(orderInfo.getTotalAmount());
            paymentInfo.setPaymentStatus(0);                  // 支付状态

            // 保存数据到数据库
            paymentInfoMapper.save(paymentInfo);
        }
        return paymentInfo;
    }

    /**
     * 支付成功后 进行相应的更新操作
     * @param paramMap
     */
    @Transactional
    @Override
    public void updatePaymentInfo(Map<String, String> paramMap) {
        // 获取订单编号
        String orderNo = paramMap.get("out_trade_no");
        // 获取到相应的支付记录
        PaymentInfo paymentInfo = paymentInfoMapper.findPaymentInfoByOrderNo(orderNo);
        if (paymentInfo.getPaymentStatus() == 1){
            // 如果相应的支付记录的订单状态是1，表示已经支付了，则直接返回 不需要再修改
            return;
        }

        // 修该支付记录的订单状态
        paymentInfo.setPaymentStatus(1);
        // 设置交易编号
        paymentInfo.setOutTradeNo(paramMap.get("trade_no"));
        // 回调时间
        paymentInfo.setCallbackTime(new Date());
        // 回调信息
        paymentInfo.setCallbackContent(paramMap.toString());
        // 修改支付记录
        paymentInfoMapper.updatePaymentInfo(paymentInfo);

        // todo: 远程调用 修改商品库存 销量
        // 现根据orderNo获取到订单信息
        OrderInfo orderInfo = orderFeignClient.getOrderInfoByOrderNo(orderNo);
        // 拿到订单项集合
        List<SkuSaleDto> skuSaleDtoList = new ArrayList<>();
        List<OrderItem> orderItemList = orderInfo.getOrderItemList();
        for (OrderItem orderItem : orderItemList) {
            SkuSaleDto skuSaleDto = new SkuSaleDto();
            // 设置商品skuId
            skuSaleDto.setSkuId(orderItem.getSkuId());
            // 设置商品销量
            skuSaleDto.setNum(orderItem.getSkuNum());
            skuSaleDtoList.add(skuSaleDto);
        }
        // 远程调用进行修改
        productFeignClient.updateProductSku(skuSaleDtoList);

        // todo: 远程调用 更新订单支付状态
        orderFeignClient.updateOrderInfoStatusPayed(orderNo);

    }
}
